<template>
  <div class="page-container">
    <SearchForm
      :search-list="searchList"
      :search-params="searchParams"
      @search="onSearch"
      @reset="onReset"
    />
    <el-form>
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        height="100%"
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label" >
          <template slot-scope="scope">
            <el-image v-if="item.type === 'image'" :preview-src-list="(scope.row.post_url || '').split(',')" :src="scope.row.post_url" style="width: 30px;height: 30px;border: 1px solid #3CAA49" fit="contain" />
            <span v-else-if="item.type === 'select'">
              {{ item.mapping[scope.row[item.prop]] }}
            </span>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="150px">
          <template slot-scope="scope">
            <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
            <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <Add ref="addFormRef" @get-page="initData" />

    <el-dialog
        fullscreen
        title="发送优惠券"
        :visible.sync="dialogValue"
        :close-on-click-modal="false"
        append-to-body
    >
      <el-form>
        <el-form-item label="发送方式">
          <el-radio-group v-model="formData.sendType">
            <el-radio-button label="1">全部用户</el-radio-button>
            <el-radio-button label="2">指定用户</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户列表" v-show="formData.sendType === '2'">
          <el-form-item>
            <div style="text-align: right">
              <el-button type="primary" @click="onSend">发送</el-button>
            </div>
          </el-form-item>
          <el-table
              v-loading="loading2"
              height="100%"
              :data="tableData2"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect2"
          >
            <el-table-column v-for="item in columns2" :key="item.prop" :prop="item.prop" :label="item.label">
              <template slot-scope="scope">
                <el-image v-if="item.type === 'image'" :preview-src-list="(scope.row[item.prop] || '').split(',')" :src="scope.row[item.prop]" style="width: 30px;height: 30px;border: 1px solid #3CAA49" fit="contain" />
                <span v-if="item.type === 'select'">
              {{ item.mapping[scope.row[item.prop]] }}
            </span>
                <span v-else>{{ scope.row[item.prop] }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http } from '@/api'
import Add from './add.vue'
export default {
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.coupon,
      searchList: [
        {
          key: 'name',
          name: 'input',
          label: '名称'
        },
        {
          key: 'start_time',
          name: 'datetime',
          label: '开始时间',
          dateType: 'date'
        },
        {
          key: 'end_time',
          name: 'datetime',
          label: '结束时间',
          dateType: 'date'
        },
        {
          key: 'status',
          name: 'select',
          label: '状态',
          options: [
            {name: '全部', value: ''},
            {name: '启用', value: 1},
            {name: '禁用', value: 0}
          ]
        },
      ],
      searchParams: {
        name: '',
        start_time: '',
        end_time: '',
        status: ''
      },
      columns: [
        { prop: 'name', label: '名称' },
        { prop: 'amount', label: '金额' },
        { prop: 'start_time', label: '开始时间' },
        { prop: 'end_time', label: '结束时间' },
        { prop: 'status', label: '状态', type: 'select', mapping: { 1: '启用', 0: '禁用' } },
        { prop: 'create_time', label: '创建时间' }
      ],
      tableData: [
      ],
      formData: {
        sendType: '1'
      },
      loading2: false,
      dialogValue: false,
      columns2: [
        { prop: 'username', label: '用户名' },
        { prop: 'email', label: '邮箱' },
        { prop: 'create_time', label: '注册时间' }
      ],
      tableData2: [
      ],
      selectOptions2: []
    }
  },
  methods: {
    formatTableData(data) {
      data.forEach(el => {

      })
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(row) {
      this.$refs.addFormRef.open(row)
    },
    onSelect2(options) {
      this.selectOptions2 = options
    },
    onSend() {
      this.dialogValue = false
    },
  }
}
</script>

<style scoped lang="scss">
</style>
